package com.ecommerce.dal.common;

import java.util.Date;
import java.util.List;

import com.ecommerce.dal.entity.Customer;
import com.ecommerce.dal.entity.CustomerItem;
import com.ecommerce.dal.entity.CustomerItemId;
import com.ecommerce.dal.entity.Item;
import com.ecommerce.dal.service.impl.CustomerManagerService;
import com.ecommerce.dal.service.impl.ItemManagerService;

public class TestCustomerItem {

	public TestCustomerItem(CustomerManagerService customerManagerService,
			ItemManagerService itemManagerService) {
		/**
		 * Make sure to switch to EAGER fetching strategy for this test to work
		 * , because of the many-to-many dependency
		 */

		// increment here
		long id = 12;
		Customer customer = new Customer();
		customer.setFirstName("Customer 1");
		customer.setUserName("Etch" + id);
		customer.setPassword("123");
		customer.setLastName("Zag");
		customer.setNewsletter(true);
		customer.setBirthDate(new Date());
		customer.setGender("male");
		customer.setJoinDate(new Date());

		customerManagerService.saveCustomer(customer);
		// Customer customer = customerManagerService.retrieveCustomer(6L);
		Item item = itemManagerService.retrieveItem(1L);
		CustomerItem customerItem = new CustomerItem();

		/** Build customerItemId manually */
		CustomerItemId customerItemId = new CustomerItemId();
		customerItemId.setCustomerId(customer.getUserId());
		customerItemId.setItemId(item.getItemId());
		/** -------------------------------- */

		/** inserting customerItem */
		customerItem.setCustomer(customer);
		customerItem.setItem(item);
		customerItem.setId(customerItemId);

		customerItem.setDiscount(item.getItemDiscount());
		customerItem.setSelectionDate(new Date());
		long verification = (long) (Math.random() * (Math.pow(10, 10)));
		customerItem.setVerification(verification);

		customerManagerService.saveCustomerItem(customerItem);
		/** -------------------------------- */

		/** retrieve customer By Verification */
		Customer customer2 = customerManagerService
				.retrieveCustomerByVerification(verification);
		System.out.println("retrieve customer By Verification:\n\n"
				+ "Customer is: " + customer2.getUserName());

		/** -------------------------------- */
		/** retrieve customerItem By customerId */
		List<CustomerItem> customerItems = customerManagerService
				.retrieveCutomerItems(id);
		for (CustomerItem customerItemX : customerItems) {
			System.out.println("retrieve customerItem By customerId:\n\n"
					+ "Customer is: "
					+ customerItemX.getCustomer().getUserName()
					+ " , and his item is "
					+ customerItemX.getItem().getItemName()
					+ " , his verification is "
					+ customerItemX.getVerification());
		}
		/** -------------------------------- */
		/** retrieve customerItem By customer verification */
		CustomerItem customerItemByVerification = customerManagerService
				.retrieveItemByVerification(verification);
		System.out
				.println("retrieve customerItem By customer verification 1:\n\n"
						+ "Customer is: "
						+ customerItemByVerification.getCustomer().getUser()
								.getUserName()
						+ " , and his item is "
						+ customerItemByVerification.getItem().getItemName());
		/** -------------------------------- */
		CustomerItem customerItemByVerificationAndId = customerManagerService
				.retrieveItemByVerification(id, verification);
		System.out
				.println("retrieve customerItem By customer verification 2:\n\n"
						+ "Customer is: "
						+ customerItemByVerificationAndId.getCustomer()
								.getUser().getUserName()
						+ " , and his item is "
						+ customerItemByVerificationAndId.getItem()
								.getItemName());
		/** -------------------------------- */
		CustomerItem customerItemByVerificationAndName = customerManagerService
				.retrieveItemByVerification("Etch"+id, verification);
		System.out
				.println("retrieve customerItem By customer verification 3:\n\n"
						+ "Customer is: "
						+ customerItemByVerificationAndName.getCustomer()
								.getUser().getUserName()
						+ " , and his item is "
						+ customerItemByVerificationAndName.getItem()
								.getItemName());

	}

}
